(function () {
	var PurchasingsPage = Spine.Controller.sub({
		init: function () {
			if (!this.layer) throw "layer required";

			if (!this.background) throw "background required";
			this.layer.add(this.background);

			if (!this.title) throw "title required";
			this.layer.add(this.title);

			if (!this.closeButton) throw "closeButton required";
			this.closeButton.on('click', this.proxy(this.close));
			this.layer.add(this.closeButton);

			if (!this.pigButtons) throw "pigButtons required";
			for (var pigButton in this.pigButtons) {
				pigButton.on('click', this.proxy(buyPig));
				this.layer.add(pigButton);
			}
			if (!this.nextPigButton) throw "nextPigButton required";
			this.nextPigButton.on('click', this.proxy(this.nextPig));
			this.layer.add(this.nextPigButton);

			if (!this.previousPigButton) throw "previousPigButton required";
			this.previousPigButton.on('click', this.proxy(this.previousPig));
			this.layer.add(this.previousPigButton);

			if (!this.accessoryButtons) throw "accessoryButtons required";
			for (var accessoryButton in this.accessoryButtons) {
				accessoryButton.on('click', this.proxy(this.buyAccessory));
				this.layer.add(accessoryButton);
			}
			if (!this.nextAccessoryButton) throw "nextAccessoryButton required";
			this.nextAccessoryButton.on('click', this.proxy(this.nextAccessory));
			this.layer.add(this.nextAccessoryButton);

			if (!this.previousAccessoryButton) throw "previousAccessoryButton required";
			this.previousAccessoryButton.on('click', this.proxy(this.previousAccessory));
			this.layer.add(this.previousAccessoryButton);

			if (!this.stage) throw "stage required";
			this.stage.add(this.layer);
		},
		close: function () {
			this.trigger('close');
		},
		buyPig: function () {
			var canBuy;
			if (canBuy) {
				this.trigger('showBuyPigConfirmDialog');
			}
			else {

			}
		},
		nextPig: function () {

		},
		previousPig: function () {

		},
		buyAccessory: function () {

		},
		nextAccessory: function () {

		},
		previousAccessory: function () {

		}
	});
	PurchasingsPage.include(Spine.Events);
	window.initPurchasingsPage = function (app, position, size) {

		var layer = new Kinetic.Layer();
		layer.add(createRect(0, 0, size.width - 20, size.height - 20));
		layer.add(createText(20, 20, 30, 'Покупки'));
		layer.add(createCloseButton(app, size));
		var pigNames = ['свинка 1', 'свинка 2', 'свинка 3', 'свинка 4'];
		var currPigIndex = 0;
		var pigs = [];
		for (var pigName in pigNames) {
			pigs.push(createPig(pigName));
		}
		layer.add(pig());
		layer.add(nextPig());
		layer.add(previousPig());
		layer.add(accessories());
		layer.add(nextAccessories());
		layer.add(previousAccessories());
		var stage = new Kinetic.Stage({
            container: 'purchasingsPage',
            x: position.x,
            y: position.y,
            width: position.x + size.width,
            height: position.y + size.height
        });
        stage.add(layer);
        return stage;
	};
	function createCloseButton (app, size) {
		var button = createButton(size.width - 140, 20, 100, 40, 'Закрыть');
		button.on('click', function () {
			app.moveToBottom('purchasingsPage');
		});
		return button;
	}
	function pig () { return createPig('Свинка 1'); }
	function createPig (name) {
		var x = 40, y = 80, width = 100, height = 100;
		var button = new Kinetic.Group({name: name});
		button.add(createRect(x, y, width, height));
		button.add(createText(x, y, 20, name));
		button.on('mouseover', function() {
			document.body.style.cursor = 'pointer';
		});
		button.on('mouseout', function() {
			document.body.style.cursor = 'default';
		});
		return button;
	}
	function nextPig (pigs, currPigIndex) {
		var b = createButton(160, 80, 210, 40, 'Следующая Свинка');
		b.on('click', function () {
			if (currPigIndex >= pigs.Length - 1) {
				return;
			}
		});
		return b;
	}
	function previousPig () {
		return createButton(160, 140, 210, 40, 'Предыдушая Свинка');	
	}
	function accessories () {
		return createButton(40, 240, 100, 100, 'Бантик 1');
	}
	function nextAccessories () {
		return createButton(160, 240, 210, 40, 'Следующая штучка');
	}
	function previousAccessories () {
		return createButton(160, 300, 210, 40, 'Предыдушая штучка');
	}

	function createButton (x, y, width, height, text) {
		var button = new Kinetic.Group();
		button.add(createRect(x, y, width, height));
		button.add(createText(x, y, 20, text));
		button.on('mouseover', function() {
			document.body.style.cursor = 'pointer';
		});
		button.on('mouseout', function() { document.body.style.cursor = 'default'; });
		return button;
	}
	function createRect (x, y, width, height) {
		return window.EngineWrapper.rect(x, y, width, height);
	}
	function createText (x, y, fontSize, text) {
		var el = new Kinetic.Text({
			x: x,
			y: y,
			text: text,
			fontSize: fontSize,
			fill: '#555',
			padding: 10,
			align: 'center'
		});
		return el;
	}
})();